package com.gjzhao.fmmall.service.juc;

import java.util.concurrent.TimeUnit;

/**
 * volatile保证可见性
 *
 */
public class TestJMMVisibility {

    public static void main(String[] args) {

        //资源类
        OrderForJMM orderForJMM = new OrderForJMM();

        new Thread(() -> {

            System.out.println("线程：" + Thread.currentThread().getName() + "，开始执行任务-----");

            // 线程睡眠3秒
            try {

                TimeUnit.SECONDS.sleep(3);

                orderForJMM.setNumber();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();

        //模拟线程B：一直在这里等待循环，直到 number 的值不等于零
        while (orderForJMM.number == 0) {

        }

        //只要变量的值被修改，就会执行下面的语句
        System.out.println("主线程: " + Thread.currentThread().getName() + "，执行任务结束");
    }
}